/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mx.netbrain.bean;

import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import mx.netbrain.dao.TreeDao;
import mx.netbrain.dao.impl.TreeDaoImpl;
import mx.netbrain.model.Tree;
import mx.netbrain.model.User;
import mx.netbrain.util.CreateFolderUtil;
import mx.netbrain.util.FileUploadUtil;
import mx.netbrain.util.FolderDataUtil;
import org.primefaces.event.FileUploadEvent;
import org.primefaces.event.NodeCollapseEvent;
import org.primefaces.event.NodeExpandEvent;
import org.primefaces.event.NodeSelectEvent;
import org.primefaces.event.NodeUnselectEvent;
import org.primefaces.model.DefaultTreeNode;
import org.primefaces.model.TreeNode;
import org.primefaces.model.UploadedFile;

/**
 *
 * @author Julian
 */
public class TreeBean implements Serializable {

    /**
     * Creates a new instance of TreeBean
     */
    private TreeNode rootNode;
    private TreeNode selectedNode;
    private TreeDao treeDao = new TreeDaoImpl();
    private User user = new User();
    private Tree tree = new Tree();
    private Tree treeEdit = new Tree();
    private Tree[] selectedTrees;
    private List<Tree> listFolder = new ArrayList<Tree>();
    private FolderDataUtil mediumFolderModel;
    private String nameFolder;
    private UploadedFile file;

    public TreeBean() {
        FacesContext session = FacesContext.getCurrentInstance();
        user = (User) session.getExternalContext().getSessionMap().get("user");
        rootNode = new DefaultTreeNode("ROOT", null);
        List<Tree> listTree = treeDao.listAll(user);
        for (Tree treeFor : listTree) {
            TreeNode t1 = createTree(treeFor, rootNode);
        }
        mediumFolderModel = new FolderDataUtil(listFolder);
    }

    public TreeNode createTree(Tree node, TreeNode rootNode) {
        TreeNode newNode = new DefaultTreeNode(node, rootNode);
        List<Tree> listTree = treeDao.listNodeId(node.getId(), user);
        for (Tree treeFor : listTree) {
            TreeNode newNode2 = createTree(treeFor, newNode);
        }
        return newNode;
    }

    public TreeNode getRootNode() {
        return rootNode;
    }

    public void setRootNode(TreeNode node) {
        this.rootNode = node;
    }

    public TreeNode getSelectedNode() {
        return selectedNode;
    }

    public void setSelectedNode(TreeNode selectedNode) {
        this.selectedNode = selectedNode;
    }

    public List<Tree> getListFolder() {
        return listFolder;
    }

    public void setListFolder(List<Tree> listFolder) {
        this.listFolder = listFolder;
    }

    public Tree[] getSelectedTrees() {
        return selectedTrees;
    }

    public void setSelectedTrees(Tree[] selectedTrees) {
        this.selectedTrees = selectedTrees;
    }

    public FolderDataUtil getMediumFolderModel() {
        return mediumFolderModel;
    }

    public String getNameFolder() {
        return nameFolder;
    }

    public void setNameFolder(String nameFolder) {
        this.nameFolder = nameFolder;
    }

    public Tree getTreeEdit() {
        return treeEdit;
    }

    public void setTreeEdit(Tree treeEdit) {
        this.treeEdit = treeEdit;
    }

    public void onNodeExpand(NodeExpandEvent event) {
        tree = (Tree) event.getTreeNode().getData();
        //FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Expanded", tree.getName());
        //FacesContext.getCurrentInstance().addMessage(null, message);
    }

    public void onNodeCollapse(NodeCollapseEvent event) {
        tree = (Tree) event.getTreeNode().getData();
//        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Collapsed", tree.getName());
//        FacesContext.getCurrentInstance().addMessage(null, message);
    }

    public void onNodeSelect(NodeSelectEvent event) throws IOException {
        tree = (Tree) event.getTreeNode().getData();
        listFolder = treeDao.listNodeFolder(tree.getId(), user);
        mediumFolderModel = new FolderDataUtil(listFolder);
    }

    public void onNodeUnselect(NodeUnselectEvent event) {
        tree = (Tree) event.getTreeNode().getData();
//        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Unselected", tree.getName());
//        FacesContext.getCurrentInstance().addMessage(null, message);
    }

    public void selectedRow() {
        for (int i = 0; i < selectedTrees.length; i++) {
            System.out.println(selectedTrees[i].getName());
            prepareTree(selectedTrees[i].getId());
        }
    }

    public void prepareTree(Long id) {
        treeEdit = treeDao.findById(id);
        System.out.println(treeEdit.getName());
    }

    public void newFolder() {
        CreateFolderUtil.createFolderDataBase(tree, nameFolder, user);
        listFolder = treeDao.listNodeFolder(tree.getId(), user);
        mediumFolderModel = new FolderDataUtil(listFolder);
        rootNode = new DefaultTreeNode("ROOT", null);
        List<Tree> listTree = treeDao.listAll(user);
        for (Tree treeFor : listTree) {
            TreeNode t1 = createTree(treeFor, rootNode);
        }
        nameFolder = new String();
    }

    public void editFolder() {
        if (treeEdit.getType().equals("Carpeta")) {
            CreateFolderUtil.editFolderDataBase(treeEdit);
            listFolder = treeDao.listNodeFolder(tree.getId(), user);
            mediumFolderModel = new FolderDataUtil(listFolder);
            rootNode = new DefaultTreeNode("ROOT", null);
            List<Tree> listTree = treeDao.listAll(user);
            for (Tree treeFor : listTree) {
                TreeNode t1 = createTree(treeFor, rootNode);
            }
            treeEdit = new Tree();
        } else {
            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error", treeEdit.getName() + ": No es una carpeta");
            FacesContext.getCurrentInstance().addMessage(null, message);
        }
    }

    public void deleteFolder() {
        if (treeEdit.getType().equals("Carpeta")) {
            CreateFolderUtil.deleteFolderDataBase(treeEdit);
            listFolder = treeDao.listNodeFolder(tree.getId(), user);
            mediumFolderModel = new FolderDataUtil(listFolder);
            rootNode = new DefaultTreeNode("ROOT", null);
            List<Tree> listTree = treeDao.listAll(user);
            for (Tree treeFor : listTree) {
                TreeNode t1 = createTree(treeFor, rootNode);
            }
            treeEdit = new Tree();
        } else {
            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error", treeEdit.getName() + ": No es una carpeta");
            FacesContext.getCurrentInstance().addMessage(null, message);
        }
    }

    public void fileUpload(FileUploadEvent event) {
        file = event.getFile();
        FileUploadUtil.uploadValidate(file, tree.getRoute(), tree.getId(), user);
        listFolder = treeDao.listNodeFolder(tree.getId(), user);
        mediumFolderModel = new FolderDataUtil(listFolder);
        rootNode = new DefaultTreeNode("ROOT", null);
        List<Tree> listTree = treeDao.listAll(user);
        for (Tree treeFor : listTree) {
            TreeNode t1 = createTree(treeFor, rootNode);
        }
    }

    public void editFile() {
        FileUploadUtil.editFile(treeEdit);
        listFolder = treeDao.listNodeFolder(tree.getId(), user);
        mediumFolderModel = new FolderDataUtil(listFolder);
        rootNode = new DefaultTreeNode("ROOT", null);
        List<Tree> listTree = treeDao.listAll(user);
        for (Tree treeFor : listTree) {
            TreeNode t1 = createTree(treeFor, rootNode);
        }
    }
}
